<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>doh.robot Toolbar Test</title>

		<style>
			@import "../../../util/doh/robot/robot.css";
		</style>

		<!-- required: dojo.js -->
		<script type="text/javascript" src="../../../dojo/dojo.js"
			djConfig="isDebug: true"></script>

		<script type="text/javascript">
			dojo.require("dijit.dijit"); // optimize: load dijit layer
			dojo.require("dijit.robotx");

			dojo.addOnLoad(function(){
				doh.robot.initRobot('../test_Toolbar.html');

				var toolbar1Before, toolbar1After, toolbar1, toolbar2, toolbar3, toolbar4;

				doh.register("initial conditions",[
					{
						name: "creation",
						setUp: function(){
							toolbar1Before = dojo.byId("toolbar1Before");
					    	toolbar1 = dijit.byId("toolbar1");
							toolbar1After = dojo.byId("toolbar1After");
					    	toolbar2 = dijit.byId("toolbar2");
					    	toolbar3 = dijit.byId("toolbar3");
					    	toolbar4 = dijit.byId("toolbar4");							
						},
						runTest: function(){
							// make sure that all the toolbars exist
							doh.t(toolbar1, "toolbar 1");
							doh.t(toolbar2, "toolbar 2");
							doh.t(toolbar3, "toolbar 3");
							doh.t(toolbar4, "toolbar 4");
							
							// and that labels are shown except when showLabel=true
							var cutText = dojo.query(".dijitButtonText", dojo.byId("toolbar1.cut"))[0];
							doh.is(0, cutText.offsetWidth, "cut button - text hidden");						

							var copyText = dojo.query(".dijitButtonText", dojo.byId("toolbar1.copy"))[0];
							doh.t(copyText.offsetWidth > 0, "copy button - text shown");						
						}
					}
				]);
					
				doh.register("keyboard",[
					{
						name: "tab in and out",
						timeout: 20000,
						runTest: function(){
							var d = new doh.Deferred();

							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								toolbar1Before.focus();
							})), 500);

							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.cut", dojo.global.dijit._curFocus.id, "cut, first visit");
							})), 1000);

							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1After", dojo.global.dijit._curFocus.id);
							})), 1000);

							doh.robot.keyPress(dojo.keys.TAB, 500, {shift: true});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.cut", dojo.global.dijit._curFocus.id, "cut, second visit");
							})), 1000);

							doh.robot.keyPress(dojo.keys.TAB, 500, {shift: true});
							doh.robot.sequence(d.getTestCallback(dojo.hitch(this, function(){
								doh.is("toolbar1Before", dojo.global.dijit._curFocus.id, "back before toolbar1");
							})), 1000);

							return d;
						}
					},
	
					{
						name: "left/right arrow keys",
						timeout: 40000,
						runTest: function(){
							var d = new doh.Deferred();

							// Disable a bunch of buttons to make sure that left/right arrow
							// keys skip over disabled buttons, and also that initial focus
							// goes to the first enabled button
							dojo.forEach(["toolbar1.cut", "toolbar1.copy", "toolbar1.bold", "toolbar1.backcolor",
								"toolbar1.forecolor", "toolbar1.combo2"], function(widgetName){
								dijit.byId(widgetName).attr("disabled", true);
							});

							// Initial focus (upon tabbing into toolbar) should go to first enabled button
							toolbar1Before.focus();
							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.italic", dojo.global.dijit._curFocus.id, "cut, first visit");
							})), 1000);

							// Remaining enabled positons that left/right arrow should navigate to (not including the initial leftmost button),
							var focusPoints = ["toolbar1.dialog", "toolbar1.combo_button", "toolbar1.combo_arrow", "toolbar1.insertorderedlist"];

							// Use right arrow key to visit every enabled button (and for combobutton to hit left and right sides)
							dojo.forEach(focusPoints, function(str){
								doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 500, {});
								doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
									doh.is(str, dojo.global.dijit._curFocus.id, str + ", first visit");
								})), 1000);
							});

							// Now go backwards
							focusPoints.reverse();
							focusPoints.shift();
							dojo.forEach(focusPoints, function(str){
								doh.robot.keyPress(dojo.keys.LEFT_ARROW, 500, {});
								doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
									doh.is(str, dojo.global.dijit._curFocus.id, str + ", second visit");
								})), 1000);
							});

							// Test the TAB key takes us to the <input> after the toolbar.
							// This confirms that the currently focused toolbar button
							// is in the correct position in the tab order (ie, the position specified
							// for the toolbar itself), and that there are no stray tabstops on the toolbar itself
							// or on the other buttons. 
							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1After", dojo.global.dijit._curFocus.id, "first time past toolbar1");
							})), 1000);

							// Make sure that we can still shift-tab back to before the toolbar too
							doh.robot.keyPress(dojo.keys.TAB, 500, {shift: true});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.italic", dojo.global.dijit._curFocus.id, "cut, after shift-tab back into toolbar");
							})), 1000);

							doh.robot.keyPress(dojo.keys.TAB, 500, {shift: true});
							doh.robot.sequence(d.getTestCallback(dojo.hitch(this, function(){
								doh.is("toolbar1Before", dojo.global.dijit._curFocus.id, "back before toolbar1");
							})), 1000);

							return d;
						}
					},

					// Test drop down ability.   These tests are probably redundant with the Button tests themselves,
					// so they aren't strictly necessary.
					{
						name: "drop downs",
						timeout: 60000,
						runTest: function(){
							var d = new doh.Deferred();

							// Re-enable the ToolipDialog and ColorPalette drop down so that we can test it
							dijit.byId("toolbar1.dialog").attr("disabled", false);
							dijit.byId("toolbar1.backcolor").attr("disabled", false);

							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								toolbar1Before.focus();
							})), 500);

							// Tab into toolbar and move to tooltip dialog button
							doh.robot.keyPress(dojo.keys.TAB, 500, {});
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 500, {});

							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.dialog", dojo.global.dijit._curFocus.id, "dialog button, first visit");
							})), 1000);

							// Open the dialog, focus should go to first input
							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 500, {});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("user", dojo.global.dijit._curFocus.id);
							})), 1000);

							// ESC should restore focus to toolbar dialog button
							doh.robot.keyPress(dojo.keys.ESCAPE, 500, {});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.dialog", dojo.global.dijit._curFocus.id, "dialog button, second visit");
							})), 1000);

							// Open the dialog again
							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 500, {});
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("user", dojo.global.dijit._curFocus.id, "user, again");
							})), 1000);

							// Submit should also restore focus to toolbar dialog button, assuming that it
							// doesn't reset the focus somewhere else (like into the editor)
							doh.robot.keyPress(dojo.keys.TAB, 500);
							doh.robot.keyPress(dojo.keys.TAB, 500);
							doh.robot.keyPress(dojo.keys.ENTER, 500);
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.dialog", dojo.global.dijit._curFocus.id, "dialog button, third visit");
							})), 1000);

							// Try the ColorPalette
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 500);
							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 500);
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.t(dojo.isDescendant(dojo.global.dijit._curFocus, dijit.byId("toolbar1.colorPalette").domNode),
									"focus inside colorpalette, actual focus is: " + (dojo.global.dijit._curFocus ? 
										(dojo.global.dijit._curFocus.id||"no id") : "no focus"));
							})), 1000);
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 500);	// navigation in the ColorPalette
							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 500);	// navigation in the ColorPalette
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.t(dojo.isDescendant(dojo.global.dijit._curFocus, dijit.byId("toolbar1.colorPalette").domNode),
									"focus still inside colorpalette, actual focus is: " + (dojo.global.dijit._curFocus ? 
										(dojo.global.dijit._curFocus.id||"no id") : "no focus"));
							})), 1000);
							
							// pressing tab in the colorpalette should move focus back to the toolbar
							doh.robot.keyPress(dojo.keys.TAB, 500);
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								doh.is("toolbar1.backcolor", dojo.global.dijit._curFocus.id, "back on colorpalette button");
							})), 1000);

							// try the ComboButton
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 500);
							doh.robot.keyPress(dojo.keys.RIGHT_ARROW, 500);
							doh.robot.keyPress(dojo.keys.DOWN_ARROW, 500);
							doh.robot.sequence(d.getTestErrback(dojo.hitch(this, function(){
								// TODO: test that focus is on menu
							})), 1000);
							doh.robot.keyPress(dojo.keys.SPACE, 500);	// select first menu option
							doh.robot.sequence(d.getTestCallback(dojo.hitch(this, function(){
								// TODO: test that focus is returned to button
							})), 1000);

							return d;
						}
					}

				]);
				// TODO: mouse
				
				
				doh.run();
			});
		</script>
	</head>
</html>
